"""
 encoding    : utf-8
 @File       : submit_survey_page.py
 @Author     : Rao
 @Version    : 1.0
 @Description: 发布调查页面
 @Date       : 2024/06/15
"""
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from pages.basepage import *


class SubmitSurveyPage(BasePage):
    """发布调查页面, 设置了成功和失败的断言"""
    # def switch_to_main_frame(self):
    #     """切换到主iframe"""
    #     main_frame = WebDriverWait(self.driver, 10).until(
    #         EC.presence_of_element_located(
    #             (
    #                 By.ID,
    #                 "iframe_main"
    #             )
    #         )
    #     )
    #     self.driver.switch_to.frame(main_frame)

    def switch_to_editor_frame(self):
        """切换到编辑器iframe"""
        editor_frame = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (
                    By.ID,
                    "baidu_editor_0"
                )
            )
        )
        self.driver.switch_to.frame(editor_frame)

    def find_title(self):
        """调查标题"""
        return self.driver.find_element(
            By.XPATH,
            '//*[@id="subject"]'
        )

    def find_survey_category(self):
        """调查类别"""
        return Select(
            self.driver.find_element(
                By.ID,
                "categoryId"
            )
        )

    def find_release_scope(self):
        """发布范围"""
        return self.driver.find_element(By.ID, "trackInput")

    def check_all_people(self):
        """发布范围-全部员工"""
        return self.driver.find_element(
            By.XPATH,
            '//*[@id="selectEmpOptionDiv"]/div[1]/button'
        )

    def find_editor_body(self):
        """文本输入框"""
        return self.driver.find_element(By.CSS_SELECTOR, "body")

    def find_add_button(self):
        """添加按钮"""
        return self.driver.find_element(
            By.XPATH,
            "//div[@id='voteDiv']//tr[2]//button"
        )

    def find_project_name(self):
        """项目名称"""
        return self.driver.find_element(By.XPATH, "//*[@id='itemName_1']")

    def find_select_item(self):
        """选择内容提示框"""
        return self.driver.find_element(
            By.XPATH,
            '//*[@id="voteitem_1_1"]'
        )

    def find_other_select(self):
        """其他选项"""
        return self.driver.find_element(
            By.NAME,
            "haveOther_1"
        )

    def find_submit_button(self):
        """发布按钮"""
        return self.driver.find_element(By.ID, "button_send")

    def find_submit_msg(self):
        return self.driver.find_element(
            By.CSS_SELECTOR,
            '[style="max-width:440px;"]'
        )


class SubmitSurveyAction:
    """对页面元素的动作"""
    def __init__(self, page, driver):
        self.page = page
        self.driver = driver

    def write_title(self, title):
        """填写调查标题"""
        # 进入frame
        self.page.switch_to_main_frame()
        if title:
            # 填写标题
            self.page.find_title().send_keys(title)
        else:
            pass

    def choose_category(self, num):
        """选择调查类别"""
        if num:
            # 选择调查类别
            self.page.find_survey_category().select_by_index(num)
        else:
            pass

    def choose_release_scope(self):
        """选择发布范围"""
        # 点击发布范围
        self.page.find_release_scope().click()
        # 全部员工
        self.page.check_all_people().click()

    def write_content(self, content):
        """输入调查内容"""
        # 进入frame2
        self.page.switch_to_editor_frame()
        if content:
            # 点击输入框
            self.page.find_editor_body().click()
            # 输入内容
            self.page.find_editor_body().send_keys(content)
        else:
            pass

    def add_project(self, pro_name, select_item):
        """添加调查项目"""
        # 返回上一级
        self.page.back_frame()
        # 添加调查项目
        self.page.find_add_button().click()
        if pro_name:
            # 输入项目名称
            self.page.find_project_name().send_keys(pro_name)
        if select_item:
            # 输入选项内容
            self.page.find_select_item().send_keys(select_item)
        # 点击其他选项
        self.page.find_other_select().click()

    def submit_survey(self) -> None:
        """点击发布按钮"""
        self.page.find_submit_button().click()

    def assert_right(self) -> None:
        """发布成功断言"""
        self.page.back_frame_root()
        assert self.page.find_submit_msg().text == '发布成功！', '发布失败'

    def assert_fail(self) -> None:
        """发布失败断言"""
        self.page.back_frame_root()
        assert self.page.find_submit_msg().text in (
            '请填写标题！',
            '请选择发布范围！',
            '调查项目不允许为空！',
            '请输入第1个项目的项目名称！',
            '第1个项目选项个数不允许小于2个（包括“其他选项”）！',
            '请输入第1个项目的项目名称！',
        ), '错误信息不匹配'
